<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>多标签分类的CrossEntropyLoss到底需不需要One-Hot编码 | 杨一赫的博客</title><meta name="author" content="杨一赫"><meta name="copyright" content="杨一赫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码  今天看文献发现了这个问题，还是基础不牢，查了一下午资料才搞懂。不过发现了好多其他的小点，比较方便用。  问题描述 在读某篇文章时，看到这样描述   image-20230303153657743  标签用的One-Hot编码，看自己的源码，有点奇怪 loss &#x3D; nn.CrossEntropyLos">
<meta property="og:type" content="article">
<meta property="og:title" content="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码">
<meta property="og:url" content="http://yang1he.gitee.io/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/index.html">
<meta property="og:site_name" content="杨一赫的博客">
<meta property="og:description" content="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码  今天看文献发现了这个问题，还是基础不牢，查了一下午资料才搞懂。不过发现了好多其他的小点，比较方便用。  问题描述 在读某篇文章时，看到这样描述   image-20230303153657743  标签用的One-Hot编码，看自己的源码，有点奇怪 loss &#x3D; nn.CrossEntropyLos">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png">
<meta property="article:published_time" content="2023-03-02T16:00:00.000Z">
<meta property="article:modified_time" content="2023-03-03T13:25:09.335Z">
<meta property="article:author" content="杨一赫">
<meta property="article:tag" content="深度学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://yang1he.gitee.io/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?6b9e2c1c603a1d11d43a9822bdd68c2c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":90,"position":"top","messagePrev":"距离上次更新已经","messageNext":"天本文可能会过期"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '多标签分类的CrossEntropyLoss到底需不需要One-Hot编码',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-03-03 21:25:09'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><script src="https://cdn.jsdelivr.net/npm/echarts@5.3.0/dist/echarts.min.js"></script><script src="https://cdn.jsdelivr.net/npm/echarts@4.7.0/map/js/china.min.js"></script><script src="node_modules/simplex-noise/simplex-noise.js"></script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="杨一赫的博客" type="application/atom+xml">
</head><body><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/pace-js/themes/blue/pace-theme-minimal.min.css"/><script src="https://cdn.jsdelivr.net/npm/pace-js/pace.min.js"></script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/favicon2.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">杨一赫的博客</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/about"><i class="fa-fw fa fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">多标签分类的CrossEntropyLoss到底需不需要One-Hot编码</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-02T16:00:00.000Z" title="发表于 2023-03-03 00:00:00">2023-03-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-03-03T13:25:09.335Z" title="更新于 2023-03-03 21:25:09">2023-03-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python/">python</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1
id="多标签分类的crossentropyloss到底需不需要one-hot编码">多标签分类的CrossEntropyLoss到底需不需要One-Hot编码</h1>
<blockquote>
<p>今天看文献发现了这个问题，还是基础不牢，查了一下午资料才搞懂。不过发现了好多其他的小点，比较方便用。</p>
</blockquote>
<h2 id="问题描述">问题描述</h2>
<p>在读某篇文章时，看到这样描述</p>
<figure>
<img
src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303153657743.png"
alt="image-20230303153657743" />
<figcaption aria-hidden="true">image-20230303153657743</figcaption>
</figure>
<p>标签用的One-Hot编码，看自己的源码，有点奇怪</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line">loss = nn.CrossEntropyLoss()</span><br><span class="line">loss=loss(<span class="built_in">input</span>,label)</span><br></pre></td></tr></table></figure>
<p><code>label</code>直接用的一个<span
class="math inline">\(class\times1\)</span>的一个向量啊，并没有One-hot（如果One-Hot,那我用的应该是<span
class="math inline">\(class\times
类别数K\)</span>的一个向量），<code>CrossEntropyLoss</code>是多分类的损失函数吗，这个<code>class</code>是指的多分类的"多"，我错了吗？</p>
<blockquote>
<p>先说结论：都没错，只不过公式用One
Hot，但是<code>torch</code>框架下<code>nn.CrossEntropyLoss()</code>是不需要one
hot编码的。</p>
</blockquote>
<h2 id="求证过程">求证过程</h2>
<p>其实只是要个结果上面就可以了，下面可能有点啰嗦，但是愿意看的话，散乱的小知识还蛮多的。</p>
<h3 id="官网描述">官网描述</h3>
<p>首先看一下官网的描述：</p>
<blockquote>
<p>torch.nn.CrossEntropyLoss(<em>weight=None</em>,
<em>size_average=None</em>, <em>ignore_index=- 100</em>,
<em>reduce=None</em>, <em>reduction='mean'</em>,
<em>label_smoothing=0.0</em>)</p>
</blockquote>
<p>The input is expected to contain the <strong><em>unnormalized
logits</em></strong>[^1] for each class (which do not need to be
positive or sum to 1, in general). input has to be a Tensor of size
(<em>C</em>) for unbatched input, (<em>minibatch</em>,<em>C</em>) or
(<em>minibatch</em>,<em>C</em>,<em>d</em>1,<em>d</em>2,...,*d**K*) with
K≥1 for the K-dimensional case. The last being useful for higher
dimension inputs, such as computing cross entropy loss per-pixel for 2D
images.</p>
<p><a
target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/358223959#:~:text=The%20(logit)%20vector%20of%20raw%20(non-normalized)%20predictions%20that,typically%20become%20an%20input%20to%20the%20softmax%20function.">logit,
logistic和sigmoid的区别 - 知乎 (zhihu.com)</a></p>
<p><a
target="_blank" rel="noopener" href="https://blog.csdn.net/c2250645962/article/details/106014693/">CrossEntropyLoss代码推导</a></p>
<p>没有提到One-hot编码的事情，但是明白了一个比较重要的小细节如上。并且提出一个例子</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># Example of target with class indices</span></span><br><span class="line">loss = nn.CrossEntropyLoss()</span><br><span class="line"><span class="built_in">input</span> = torch.randn(<span class="number">3</span>, <span class="number">5</span>, requires_grad=<span class="literal">True</span>)</span><br><span class="line">target = torch.empty(<span class="number">3</span>, dtype=torch.long).random_(<span class="number">5</span>)</span><br><span class="line">output = loss(<span class="built_in">input</span>, target)</span><br><span class="line">output.backward()</span><br><span class="line"></span><br><span class="line"><span class="comment"># Example of target with class probabilities</span></span><br><span class="line"><span class="built_in">input</span> = torch.randn(<span class="number">3</span>, <span class="number">5</span>, requires_grad=<span class="literal">True</span>)</span><br><span class="line">target = torch.randn(<span class="number">3</span>, <span class="number">5</span>).softmax(dim=<span class="number">1</span>)</span><br><span class="line">output = loss(<span class="built_in">input</span>, target)</span><br><span class="line">output.backward()</span><br></pre></td></tr></table></figure>
<p>这个例子也只是说label可以用含有更多信息的概率值。</p>
<p>其次是学了一套方法，生成哑变量与哑变量（或者叫one-hot编码）的回溯。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment">#生成one-hot编码</span></span><br><span class="line"><span class="keyword">import</span> torch.nn.functional <span class="keyword">as</span> F</span><br><span class="line">target = F.one_hot(torch.empty(<span class="number">3</span>, dtype=torch.long).random_(<span class="number">5</span>), num_classes=<span class="number">5</span>)   <span class="comment"># n为类别数</span></span><br><span class="line"><span class="comment">#one-hot编码回溯 用torch.full</span></span><br><span class="line">target = autograd.Variable(torch.LongTensor([<span class="number">1</span>, <span class="number">0</span>, <span class="number">4</span>]))</span><br><span class="line">labels = torch.full(size=(N, C), fill_value=<span class="number">0</span>)</span><br><span class="line">labels.scatter_(dim=<span class="number">1</span>, index=torch.unsqueeze(target, dim=<span class="number">1</span>), value=<span class="number">1</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;labels is &#123;&#125;&#x27;</span>.<span class="built_in">format</span>(labels))</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">labels is tensor([[0., 1., 0., 0., 0.],</span></span><br><span class="line"><span class="string">                  [1., 0., 0., 0., 0.],</span></span><br><span class="line"><span class="string">                  [0., 0., 0., 0., 1.]])</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>
<p>没找到能解答我疑惑的，可能是我对CrossEntropyLoss的理解不够透彻，自己出了个小例子来计算。</p>
<h3
id="crossentropyloss定义以及手撕题">CrossEntropyLoss定义，以及手撕题</h3>
<blockquote>
<p>有两个class，每个class有两个dimension，<span
class="math inline">\(\left[\begin{array}{} 1&amp; 0.5\\ 0.5&amp;1
\end{array}\right]\)</span>是他未经过softmax处理的raw logit,label是<span
class="math inline">\([1,0]\)</span>,求一下loss，用CrossEntropyLoss</p>
</blockquote>
<h3 id="解">解</h3>
<p><span class="math display">\[
CrossEntropyLoss=logSoftmax+NLLLoss
\]</span></p>
<p>所以，为了方便理解，先讲一下Logsoftmax、以及NLLLoss做一个铺垫,</p>
<p>Logsoftmax定义： <span class="math display">\[
f_i(x)=log(\frac{e^{(x_i)}}{\sum e^{x_i}})
\]</span> 就是先取softmax再log，接着是NLLlloss: <span
class="math display">\[
f(x,y)=-\frac{1}{N}\sum x_i*y_i
\]</span> NLLLoss得到损失。<span class="math inline">\(x\)</span>和<span
class="math inline">\(y\)</span>分别代表input和label，N带表的是你的classes</p>
<p>真懂了吗，解这个计算题</p>
<blockquote>
<p>有两个class，每个class有两个dimension，<span
class="math inline">\(\left[\begin{array}{} 1&amp; 0.5\\ 0.5&amp;1
\end{array}\right]\)</span>是他未经过softmax处理的raw logit,label是<span
class="math inline">\([1,0]\)</span>,求一下loss，用CrossEntropyLoss</p>
</blockquote>
<p>解：套公式；自己先手算下哈</p>
<p>step1:计算logsoftmax <span class="math display">\[
f_i(x)=\left[\begin{array}{}
log(\frac{e^1}{e^1+e^{0.5}}) &amp; log(\frac{e^0.5}{e^1+e^{0.5}})\\
log(\frac{e^{0.5}}{e^1+e^{0.5}})&amp;log(\frac{e^1}{e^1+e^{0.5}})
\end{array}\right]\\=\left[\begin{array}{}
-0.9471&amp; -0.4741\\
-0.4717&amp;-0.9471
\end{array}\right]
\]</span> step2:算NLLloss</p>
<p>首先，label给的不是one-hot,如果分两步算的话，需要先one-hot编码，label就成了<span
class="math inline">\(\left[\begin{array}{} 0&amp;1\\ 1&amp;0
\end{array}\right]\)</span>，然后 <span class="math display">\[
\begin{aligned}
Loss&amp;=-\frac{1}{2}\left([-0.9471,-0.4741]\begin{bmatrix}
0 \\
1
\end{bmatrix}+[-0.4741,-0.9471]\begin{bmatrix}
1 \\
0
\end{bmatrix}
\right)\\&amp;=0.4741
\end{aligned}
\]</span> 算对了吗同志？</p>
<blockquote>
<p>注意到了吗，这里是先做的one-hot编码</p>
</blockquote>
<p><strong><em>因此那，本质上只是用Torch的代码的时候，他帮你做了onehot编码，所以你可以忽略这一步。</em></strong></p>
<blockquote>
<p>最后：</p>
</blockquote>
<p>CrossEntropyLoss 的完整定义： <span class="math display">\[
\begin{equation} \text{loss}(x, y) = -\sum_{i=1}^{C} y_i
\log(\text{softmax}(x)_i) \end{equation}
\]</span> 其中，<span class="math inline">\(C\)</span> 是分类数，<span
class="math inline">\(y_i\)</span> 表示样本 <span
class="math inline">\(x\)</span> 的真实标签在第 <span
class="math inline">\(i\)</span> 个类别上的概率，<span
class="math inline">\(\text{softmax}(x)_i\)</span> 表示样本 <span
class="math inline">\(x\)</span> 在第 <span
class="math inline">\(i\)</span> 个类别上的预测概率。</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io">杨一赫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://yang1he.gitee.io/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/">http://yang1he.gitee.io/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://yang1he.gitee.io" target="_blank">杨一赫的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></div><div class="post_share"><div class="social-share" data-image="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/"><img class="prev-cover" src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230305093941469.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能</div></div></a></div><div class="next-post pull-right"><a href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/"><img class="next-cover" src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230228145723183.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">GPTEX---为ChatGPT而生的latex软件</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/favicon2.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">杨一赫</div><div class="author-info__description">阳光开朗大男孩</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">14</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">16</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://gitee.com/yang1he"><i class="fab fa-github"></i><span>gitee</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">平平无奇的网站</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84crossentropyloss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81one-hot%E7%BC%96%E7%A0%81"><span class="toc-number">1.</span> <span class="toc-text">多标签分类的CrossEntropyLoss到底需不需要One-Hot编码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%97%AE%E9%A2%98%E6%8F%8F%E8%BF%B0"><span class="toc-number">1.1.</span> <span class="toc-text">问题描述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B1%82%E8%AF%81%E8%BF%87%E7%A8%8B"><span class="toc-number">1.2.</span> <span class="toc-text">求证过程</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%98%E7%BD%91%E6%8F%8F%E8%BF%B0"><span class="toc-number">1.2.1.</span> <span class="toc-text">官网描述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#crossentropyloss%E5%AE%9A%E4%B9%89%E4%BB%A5%E5%8F%8A%E6%89%8B%E6%92%95%E9%A2%98"><span class="toc-number">1.2.2.</span> <span class="toc-text">CrossEntropyLoss定义，以及手撕题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3"><span class="toc-number">1.2.3.</span> <span class="toc-text">解</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/model_00.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks"/></a><div class="content"><a class="title" href="/2023/06/24/An%20Intelligent%20Mobile%20Prediction%20method/" title="An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks">An Intelligent Mobile Prediction method with Mini-batch HTIA-based Seq2Seq Networks</a><time datetime="2023-06-23T16:00:00.000Z" title="发表于 2023-06-24 00:00:00">2023-06-24</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题"><img src="/img/rick1.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="无题"/></a><div class="content"><a class="title" href="/2023/03/05/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96%E4%BD%A0%E4%B8%AA%E4%BA%BA%E8%B4%A6%E5%8F%B7%E7%9A%84openai%E7%9A%84api/" title="无题">无题</a><time datetime="2023-03-05T05:57:17.050Z" title="发表于 2023-03-05 13:57:17">2023-03-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230305093941469.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能"/></a><div class="content"><a class="title" href="/2023/03/04/%E4%B8%89%E5%88%86%E9%92%9F4%E8%A1%8C%E5%91%BD%E4%BB%A4%E6%9E%84%E5%BB%BAchatgpt%20webapp,%E6%94%AF%E6%8C%81%E9%AB%98%E5%B9%B6%E5%8F%91%E4%BB%A5%E5%8F%8A%E4%B8%8A%E4%B8%8B%E6%96%87%E5%AF%B9%E8%AF%9D%E5%8A%9F%E8%83%BD/" title="三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能">三分钟4行命令构建chatgpt webapp,支持高并发以及上下文对话功能</a><time datetime="2023-03-03T16:00:00.000Z" title="发表于 2023-03-04 00:00:00">2023-03-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230303211538791.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码"/></a><div class="content"><a class="title" href="/2023/03/03/%E5%A4%9A%E6%A0%87%E7%AD%BE%E5%88%86%E7%B1%BB%E7%9A%84CrossEntropyLoss%E5%88%B0%E5%BA%95%E9%9C%80%E4%B8%8D%E9%9C%80%E8%A6%81One-Hot%E7%BC%96%E7%A0%81/" title="多标签分类的CrossEntropyLoss到底需不需要One-Hot编码">多标签分类的CrossEntropyLoss到底需不需要One-Hot编码</a><time datetime="2023-03-02T16:00:00.000Z" title="发表于 2023-03-03 00:00:00">2023-03-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件"><img src="https://nmhjklnm.oss-cn-beijing.aliyuncs.com/article-img/img/image-20230228145723183.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="GPTEX---为ChatGPT而生的latex软件"/></a><div class="content"><a class="title" href="/2023/02/28/GPTEX---%E4%B8%BAChatGPT%E8%80%8C%E7%94%9F%E7%9A%84latex%E8%BD%AF%E4%BB%B6/" title="GPTEX---为ChatGPT而生的latex软件">GPTEX---为ChatGPT而生的latex软件</a><time datetime="2023-02-27T16:00:00.000Z" title="发表于 2023-02-28 00:00:00">2023-02-28</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By 杨一赫</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="聊天"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script><script>function addGitalkSource () {
  const ele = document.createElement('link')
  ele.rel = 'stylesheet'
  ele.href= 'https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.css'
  document.getElementsByTagName('head')[0].appendChild(ele)
}

function loadGitalk () {
  function initGitalk () {
    var gitalk = new Gitalk(Object.assign({
      clientID: '5a73aec03d8a0aeb971f',
      clientSecret: '54ad1c8adab95b382d582c73e9f597781de43525',
      repo: 'comment',
      owner: 'nmhjklnm',
      admin: ['nmhjklnm'],
      id: '409ebbd2604dfec29756dcd7967efefd',
      updateCountCallback: commentCount
    },null))

    gitalk.render('gitalk-container')
  }

  if (typeof Gitalk === 'function') initGitalk()
  else {
    addGitalkSource()
    getScript('https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js').then(initGitalk)
  }
}

function commentCount(n){
  let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
  if (isCommentCount) {
    isCommentCount.innerHTML= n
  }
}

if ('Gitalk' === 'Gitalk' || !false) {
  if (false) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
  else loadGitalk()
} else {
  function loadOtherComment () {
    loadGitalk()
  }
}</script></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="//code.tidio.co/6zvu6bgfljrvvv9rw1y7hzehoriklr03.js" async="async"></script><script>function onTidioChatApiReady() {
  window.tidioChatApi.hide();
  window.tidioChatApi.on("close", function() {
    window.tidioChatApi.hide();
  });
}
if (window.tidioChatApi) {
  window.tidioChatApi.on("ready", onTidioChatApiReady);
} else {
  document.addEventListener("tidioChat-ready", onTidioChatApiReady);
}

var chatBtnFn = () => {
  document.getElementById("chat_btn").addEventListener("click", function(){
    window.tidioChatApi.show();
    window.tidioChatApi.open();
  });
}
chatBtnFn()
</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/metingjs/dist/Meting.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>